package cn.tedu.sp04.order.service;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp04.order.fegin.ItemClient;
import cn.tedu.sp04.order.fegin.UserClient;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import service.OrderService;

import java.util.List;

@RestController
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;
    @Override
    public Order getOrder(String orderId) {
        //远程调用商品服务，获取订单的商品列表
        JsonResult<List<Item>> items=itemClient.getItems(orderId);
        //远程调用用户服务 获取用户信息
        JsonResult<User> user=userClient.getUser(8);

        Order order = new Order();
        order.setId(orderId);
        order.setUser(user.getData());
        order.setItems(items.getData());

        return order;
    }

    @Override
    public void addOrder(Order order) {
        log.info("添加订单: "+order);

        //远程调用商品服务 减少商品库存
        itemClient.decreaseNumber(order.getItems());
        //远程调用商品服务  增加用户积分
        userClient.addScore(order.getUser().getId(), 1000);
    }
}
